package com.wxtwxt.ruralmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.wxtwxt.ruralmall.common.utils.R;
import com.wxtwxt.ruralmall.product.entity.BrandEntity;
import com.wxtwxt.ruralmall.product.vo.BrandVo;
import org.springframework.web.bind.annotation.*;

import com.wxtwxt.ruralmall.product.entity.CategoryBrandRelationEntity;
import com.wxtwxt.ruralmall.product.service.CategoryBrandRelationService;
import com.wxtwxt.ruralmall.common.utils.PageUtils;

import javax.annotation.Resource;



/**
 * 品牌分类关联
 *
 * @author Wxt
 * @email wxt23787@126.com
 * @date 2023-01-30 19:53:32
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    @GetMapping("/brands/list")
    public R getBrandsList(@RequestParam(value = "catId") Long catId) {
        List<BrandEntity> brandEntities = categoryBrandRelationService.getBrandsList(catId);
        List<BrandVo> brandVos = brandEntities.stream().map(brandEntity -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(brandEntity.getBrandId());
            brandVo.setBrandName(brandEntity.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", brandVos);
    }

    /**
     * 获取品牌关联的分类
     * @param brandId 品牌ID
     */
    @GetMapping("/catelog/list")
    public R getRelationList(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelationEntity> relationList = categoryBrandRelationService.getRelationList(brandId);
        return R.ok().put("data", relationList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveRelation(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
